<?
/*
 * Copyright (c) 2010 DC Team
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files(the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

define('DC', true);
include_once('../common.php');

$act = !isset($_REQUEST['act']) ? '' : trim($_REQUEST['act']);
if ($act == '1'){
?>
<!--数据库配置信息-->
	<table width="800" align="center" cellpadding="0" cellspacing="0">
		<tr><th>tools域名</th><th>服务器名称</th><th>IP</th><th>数据库名称</th><th>用户名</th><th>密码</th><th>端口</th><th>操作</th></tr>
		<tr id='newline'><td><input type='text' name='newlang' id='newlang' size='10' value="<?php echo $_SERVER['SERVER_NAME'];?>"></td><td><input type='text' name='newname' id='newname' size='5'></td><td><input type='text' name='newhost' id='newhost' size='5'></td><td><input type='text' name='newdbname' id='newdbname' size='5'></td>
		<td><input type='text' name='newuser' id='newuser'  size='5'></td><td><input type='text' name='newpass' id='newpass' size='5'></td><td><input type='text' name='newport' id='newport' size='5'></td><td><a href='#' onclick="configHandling('adddb', 'new');">增加</a></td></tr>
		<?php
		foreach ($g_db_conf as $key => $value){
			foreach ($value as $k => $val){
				$id = uniqid();
				echo "<tr><td><input type='text' name='{$id}lang' id='{$id}lang' value='{$key}' size='10'></td><td><input type='text' name='{$id}name' id='{$id}name' value='{$k}' size='5'></td><td><input type='text' name='{$id}host' id='{$id}host' value='{$val['db_host']}' size='5'></td><td><input type='text' name='{$id}dbname' id='{$id}dbname' value='{$val['db_name']}' size='5'></td>";
				echo "<td><input type='text' name='{$id}user' id='{$id}user' value='{$val['db_user']}' size='5'></td><td><input type='text' name='{$id}pass' id='{$id}pass' value='{$val['db_pass']}'  size='5'></td><td><input type='text' name='{$id}port' id='{$id}port' value='{$val['db_port']}' size='5'></td><td><a href='#' onclick=\"configHandling('editdb', '{$id}');\">修改</a>｜<a href='#' onclick=\"if (confirm('确定删除吗?')) configHandling('deldb', '{$id}');\">删除</a></td></tr>";
				echo "<input type='hidden' name='{$id}langbak' value='{$key}'><input type='hidden' name='{$id}namebak' value='{$k}'>";
			}
		}

		?>
		<td colspan="7"><font color="red">此操作需添加域名为键名，并且将替换原有同名配置</font><br /><textarea cols="80" name="batch_db" id="batch_db"></textarea></td><td><a href='#' onclick="if (confirm('此操作将替换原有同名配置，确定执行此操作吗?')) configBatch('batch_db');">批量添加</a></td>
	</table>
<?php
}elseif ($act == '2'){
?>
  	<!--固定表配置-->
	<table width="800" align="center" cellpadding="0" cellspacing="0"><!--<tr><th colspan='4'>固定表查询设置</th></tr>-->
		<tr><td><input type='text' name='typename'></td><td><input type='text' name='typemaintable'/></td><td><input type='text' name='typetables' /></td><td><a href='#' onclick="configHandling('addtype', 'type');">增加</a></td></tr>
		<tr><th>名称</th><th>主表</th><th>副表(必须以|结尾)</th><th>操作</th></tr>
		<?php
		foreach ($configs['type'] as $key => $value){
			$id = uniqid();
			echo "<tr><td><input type='text' name='{$id}name' value='{$key}' /></td><td><input type='text' name='{$id}maintable' value='{$value[0]}' /></td><td><input type='text' name='{$id}tables' value='{$value[1]}' /></td><td><a href='#' onclick=\"configHandling('edittype', '{$id}');\">修改</a>｜<a href='#' onclick=\"if(confirm('确定删除吗?')) configHandling('deltype', '{$id}');\">删除</a></td></tr>";
			echo "<input type='hidden' name='{$id}namebak' value='{$key}'>";
		}

		?>
	</table>
<?php
}elseif ($act == '3'){
?>
  	<!--比较条数与固定同步的表设置-->
	<table width="800" align="center" cellpadding="0" cellspacing="0"><tr><th colspan='2'><!--表设置-->[<a href='#' onclick="configHandling('update', '');">更新</a>]</th></tr>
		<tr><td>比较条数的表</td><td><textarea cols='60' rows='3' id='content1'><?php echo implode(',', $configs['table']);?></textarea></td>
		<tr><td>固定同步的表</td><td><textarea cols='60' rows='3' id='content2'><?php echo implode(',', $configs['asyn']);?></textarea></td>
		<tr><td>需要运行的SQL</td><td><textarea cols='60' rows='5' id='content7'><?php echo $configs['autosql'];?></textarea></td>
		<tr><td>目录设定</td><td><textarea cols='60' rows='3' id='content3'><?php echo $configs['domain'];?></textarea></td>
		<tr><td>基础库设置</td><td><textarea cols='60' rows='3' id='content4'><?php echo $configs['maindb'];?></textarea></td>
		<tr><td>不需要对比字段类型的表</td><td><textarea cols='60' rows='3' id='content5'><?php echo implode(',', $configs['field']);?></textarea></td>
		<tr><td>不需要对比索引的表</td><td><textarea cols='60' rows='3' id='content6'><?php echo implode(',', $configs['index']);?></textarea></td>
	</table>
<?php
}elseif ($act == '4'){
?>
  	<!--用户管理-->
	<table width="800" align="center" cellpadding="0" cellspacing="0"><!--<tr><th colspan='3'>用户管理</th></tr>-->
		<tr><td><input type='text' name='username' /></td><td><input type='text' name='userpwd' /></td><td><a href='#' onclick="configHandling('adduser', 'user');">增加</a></td></tr>
		<tr><th>用户名</th><th>密码</th><th>操作</th></tr>
		<?php
		foreach ($configs['user'] as $key => $value){
			$id = uniqid();
			echo "<tr><td><input type='text' name='{$id}name' value='{$value['username']}' /></td><td><input type='text' name='{$id}pwd' value='{$value['userpwd']}' /></td><td><a href='#' onclick=\"configHandling('edituser', '{$id}');\">修改</a>｜<a href='#' onclick=\"if (confirm('确定删除吗?')) configHandling('deluser', '{$id}');\">删除</a></td></tr>";
			echo "<input type='hidden' name='{$id}namebak' value='{$value['username']}'>";
		}

		?>
	</table>
<?php
}elseif ($act == '5'){
?>
  	<!--mmc配置管理-->
	<table width="800" align="center" cellpadding="0" cellspacing="0">
		<tr><td><input type='text' name='mmcname' /></td><td><input type='text' name='mmc' /></td><td><input type='text' name='mmcunkey' /></td><td><a href='#' onclick="configMmc('addmmc', 'mmc');">增加</a></td></tr>
		<tr><th>表名</th><th>是否缓存</th><th>unkey</th><th>操作</th></tr>
		<?php
		foreach ($g_mmc_tbconf as $key => $value){
			$id = uniqid();
			if (!isset($value['unkey'])) {
				$value['unkey'] = '';
			}else {
				$value['unkey'] = implode(',', $value['unkey']);
			}
			if ($value['mmc']) {
				$value['mmc'] = 'true';
			}else {
				$value['mmc'] = 'false';
			}
			echo "<tr><td><input type='text' name='{$id}tablename' value='{$key}' /></td><td><input type='text' name='{$id}mmc' value='{$value['mmc']}' /></td><td><input type='text' name='{$id}unkey' value='{$value['unkey']}' /></td><td><a href='#' onclick=\"configMmc('editmmc', '{$id}');\">修改</a>｜<a href='#' onclick=\"if (confirm('确定删除吗?')) configMmc('delmmc', '{$id}');\">删除</a></td></tr>";
			echo "<input type='hidden' name='{$id}tablenamebak' value='{$key}'>";
		}

?>
		<td colspan="3"><font color="red">此操作将替换原有同名配置</font><br /><textarea cols="80" name="batch_mmc" id="batch_mmc"></textarea></td><td><a href='#' onclick="if (confirm('此操作将替换原有同名配置，确定执行此操作吗?')) configBatch('batch_mmc');">批量添加</a></td>
	</table>	
<?php
}elseif ($act == '6'){
	$mainTables = getMainTables();
	$arr = array();
	foreach ($configs['back'] as $key => $value){
		if ( $key == '主要表' || $key == '清档表' ){
			$arr = array_merge($arr, $value);
		}		
	}
	$mArr = array_diff($mainTables, $arr);

//	$mStr = '';
//	foreach ( $mArr as $k => $v ){
//		$mStr .= $v.' ';
//		if ( is_int($k+1/10) ){
//			$mStr .= '<br />';
//		}
//	}
?>
	<!--备份表配置-->
	<table width="800" align="center" cellpadding="0" cellspacing="0">		
		<tr><td><input type="text" name="backname"/></td><td><input type="text" name='backcontent' size="50"/></td><td><a href='#' onclick="configHandling('addback', 'back');">增加</a></td></tr>
		<tr><th>名称</th><th>表名称（多个以半角,隔开)</th><th>操作</th></tr>
		<?php
		foreach ($configs['back'] as $key => $value){
			$id = uniqid();
		?>		
			<tr><td><input type="text" size="10" name="<?php echo $id.'name';?>" value="<?php echo $key;?>" /></td><td><textarea cols="50" rows="10" name='<?php echo $id;?>content' ><?php echo implode(',', $value);?></textarea></td><td><a href='#' onclick="configHandling('editback', '<?php echo $id;?>');">修改</a>｜<a href='#' onclick="if (confirm('确定删除吗?')) configHandling('delback', '<?php echo $id;?>');">删除</a></td></tr>
			<input type="hidden" name="<?php echo $id.'namebak';?>" value="<?php echo $key;?>" />
		<?php
		}
		?>
		<tr><td>未设置的表</td><td><?php echo implode('<br />', $mArr); ?></td><td><!--<a href="#" id="showallconfigtable">查看</a>--><a href="showalltables.php" target="_blank">查看</a></td></tr>
	</table>
<?php
}
?>